﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
     
        if (!IsPostBack)
        {
            if (Request["ID"] != null)
            {
                InitData();
            }
        }
    }

    protected void InitData()
    {

        int id = int.Parse(Request["ID"].ToString());

        try
        {


            DataSet ds = new DataSet();
            ds = new Class1().hsggetdata("select * from 管理员 where id=" + id);
            if (ds.Tables[0].Rows.Count > 0)
            {

                this.TextBox1.Text = ds.Tables[0].Rows[0]["姓名"].ToString();
                this.TextBox2.Text = ds.Tables[0].Rows[0]["密码"].ToString();
                this.TextBox3.Text = ds.Tables[0].Rows[0]["密码"].ToString();

                this.DropDownList1.Text = ds.Tables[0].Rows[0]["性别"].ToString();
                this.TextBox4.Text = ds.Tables[0].Rows[0]["年龄"].ToString();
                this.TextBox5.Text = ds.Tables[0].Rows[0]["电话"].ToString();

                this.TextBox6.Text = ds.Tables[0].Rows[0]["住址"].ToString();
               
            }


        }
        catch
        {



        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql;

        if (TextBox2.Text.ToString().Trim() == "" || TextBox3.Text.ToString().Trim() == "")
        {
            Response.Write("<script>javascript:alert('请填写完整');history.back();</script>");
        }
        else
        {
            if (TextBox2.Text.ToString().Trim() != TextBox3.Text.ToString().Trim())
            {
                Response.Write("<script>javascript:alert('两次密码不一致，请确认');history.back();</script>");
            }


            if (Request["ID"] != null)
            {
                int id = int.Parse(Request["ID"].ToString());
                sql = "update 管理员 set 姓名='" + TextBox1.Text + "',密码='" + TextBox2.Text + "',性别='" + DropDownList1.SelectedItem.Text+ "',年龄='" +TextBox4.Text + "',电话='" + TextBox5.Text + "',住址='" + TextBox6.Text  + "' where id=" + id;
                int result;
                result = new Class1().hsgexucute(sql);
                if (result == 1)
                {
                    Response.Write("<script>javascript:alert('操作成功');</script>");
                }
                else
                {
                    Response.Write("<script>javascript:alert('系统错误，您没有设置好数据库的连接！或您输入了一些非法字符！');</script>");
                }

            }
            else
            {
                sql = "insert into 管理员(姓名,密码,性别,年龄,电话,住址 ) values('" + this.TextBox1.Text.Trim() + "','" + this.TextBox2.Text + "','" + DropDownList1.SelectedItem.Text + "'," + this.TextBox4.Text + "," + this.TextBox5.Text + ",'" + this.TextBox6.Text  + "')";

                int result;
                result = new Class1().hsgexucute(sql);
                if (result == 1)
                {
                    Response.Write("<script>javascript:alert('添加成功');</script>");
                }
                else
                {
                    Response.Write("<script>javascript:alert('系统错误，您没有设置好数据库的连接！');</script>");
                }
            }
        }
    }






    


 
}
